package com.cloudpivot.project.crmprojectnew.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.cloudpivot.project.crmprojectnew.configuration.CrmConfig;
import com.cloudpivot.project.crmprojectnew.pojo.task.Task;
import com.cloudpivot.project.crmprojectnew.service.HttpAPIServiceForCrm;
import com.cloudpivot.project.crmprojectnew.service.SystemusersService;
import com.cloudpivot.project.crmprojectnew.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil.convertGMTDateTime;

/**
 * 任务实现接口
 */
@Service
@Slf4j
public class TaskSerivceImpl implements TaskService {

    @Autowired
    private CrmConfig crmConfig;
    @Autowired
    private SystemusersService systemusersService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    @Override
    public Task addTask(Task task) {
        //任务api接口
        String apiStr = "tasks";
        String requestUrl = String.format(("%s%s"), crmConfig.getCrmurl(), apiStr);
        log.info("任务API请求地址:{}", requestUrl);
        //获取用户guid
        String userGuid = systemusersService.getCrmUserGuid(task.getUserinfo());
        if (userGuid == null) {
            return null;
        }
        try {
            //转换时间
            task.setScheduledstart(convertGMTDateTime(task.getScheduledstart()));
            task.setScheduledend(convertGMTDateTime(task.getScheduledend()));
            task.setActualend(convertGMTDateTime(task.getActualend()));
            task.setActualstart(convertGMTDateTime(task.getActualstart()));

            log.info("任务API请求json:{}", objectMapper.writeValueAsString(task));
            String responseStr = httpAPIServiceForCrm.doPostCrmSingle(requestUrl, objectMapper.writeValueAsString(task), userGuid);
            log.info("任务API请求返回字符串:{}", responseStr);
            Task responseTask = objectMapper.readValue(responseStr, Task.class);
            return responseTask;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    @Override
    public List<Task> getTask(String user) {
        String apiStr = "tasks";
        String requestUrl=String.format("%s%s",crmConfig.getCrmurl(),apiStr);
        log.info("任务API查询请求地址:" + requestUrl);
        String userGuid=systemusersService.getCrmUserGuid(user);
        if(userGuid==null){
            return null;
        }
        try{
            String responseStr=httpAPIServiceForCrm.doGetCrm(requestUrl,userGuid);
            log.info("查询请求返回字符串" + requestUrl);
            
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }

        return null;
    }

    @Override
    public String delTask(Task task) {
        String apiStr = "tasks";
        //获取guid
        String guid = task.getActivityid();
        String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), apiStr, guid);
        log.info("任务删除API请求地址:{}", requestUrl);
        if (guid == null || "".equals(guid)) {
            return null;
        }
        try {
            String responseStr = httpAPIServiceForCrm.doDeleteCrm(requestUrl);
            return responseStr;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
